package com.xu.base;

import lombok.NonNull;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 框架基础服务自动装配
 *
 * @Author: 许乾伟
 * @Date: 2024/12/3 14:49
 */
public class EnableAutoConfiguration {
    /**
     * 处理跨域问题
     * @return
     */
    @Bean
    public WebMvcConfigurer corsConfigure() {
        System.out.println("处理跨域问题");
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(@NonNull CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOriginPatterns("*")
                        .allowedMethods("*")
                        .allowCredentials(true);
            }

            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
            }
        };
    }
}
